layui.use(['form', 'layer', 'laydate', 'table'], function () {
    let form = layui.form;
    let layer = layui.layer;
    let laydate = layui.laydate;
    let table = layui.table;
    //获取方案一2提交接口url
    let programOne2Url = apiArr[2].trim();
    let traceCodesObj = [];
    form.verify({
        traceContent: function (value, item) { //value：表单的值、item：表单的DOM对象
                traceCodesObj = []; //每次提交时清空数组
                value = value.trim();
                let traceCodesStr = value.split('\n'); //js通过\n转义换行符
                let errorArr = []; //错误记录行号数组
                traceCodesStr.map(function (item, index) {
                    let traceCodeObj = {
                        $: {}
                    }; //构建单条追溯码内容对象
                    if (item.trim().length != 32) {
                        errorArr.push(index + 1);
                    }
                    traceCodeObj.$.value = item;
                    traceCodesObj.push(traceCodeObj);
                });
                if (errorArr.length > 0) {
                    showErrorNumber(errorArr);
                    return "追溯码内容【" + errorArr + "】行数据，格式错误";
                }
            }

            //我们既支持上述函数式的方式，也支持下述数组的形式
            //数组的两个值分别代表：[正则匹配、匹配不符时的提示文字]
            ,
        pass: [
            /^[\S]{6,12}$/, '密码必须6到12位，且不能出现空格'
        ]
    });
    //监听提交
    form.on('submit(formDemo)', function (data) {
        //加载层-风格4
        let index = layer.msg("<div class='tip-content'>系统根据方案一表单生成xml文件中，请勿关闭客户端！</div>", {
            icon: 16,
            shade: 0.01,
            time: 0
        });
        delete data.field.trace;
        filedFormat(data.field, ['outputDate']);
        //构建符合xml2js规定的xml对象，转换为字符串
        let o = {
            '$': data.field,
            packCodes: {
                packCode: traceCodesObj
            }
        };
        //json->xml
        let xmlBuilder = new xml2js.Builder({
            xmldec: {
                'version': '1.0',
                'encoding': 'UTF-8'
            },
            rootName: 'OUTPUTDATA'
        });
        xmlParserThenSubmit(o, programOne2Url, xmlBuilder, 'OUTPUTDATA', index, "case1_output");
    });

    //执行一个laydate实例
    laydate.render({
        elem: '#outputDate' //指定元素
    });

    //设置页面布局
    $pageLayout = $('body').layout({
        south__paneSelector: ".outer-south",
        center__paneSelector: ".outer-center",
        south__spacing_open: 0,
        south__size: 60
    });

    //选择农药产品登记证号
    $(".layui-icon-search").on("click", function () {
        layer.open({
            title: '选择登记证号',
            type: 2,
            area: ['1200px', '600px'],
            fixed: false, //不固定
            maxmin: true,
            //closeBtn: 1, //1 不显示关闭按钮  
            content: "../../certicateselect/html/index.html"
        });
    });
    $("#codeSource").bind("change", function () {
        if ($("#codeSource").val() == 1) {
            $("#qrSample").attr("readonly", false);
            $("#qrSample").val("");
        } else if ($("#codeSource").val() == 0) {
            $("#qrSample").val("http://q.icama.cn/q/# {tracecode}");
            $("#qrSample").attr("readonly", true);
        }
    });

    $("div.layui-form-item.outer-south").find("button.layui-btn-primary").on("click", function () {
        resetBtnStatu();
    });
    $("#trace").setTextareaCount({
        width: "30px",
        bgColor: "rgb(74, 160, 151)",
        color: "#FFF",
        display: "block"
    });
});